//
//  FMCommunityTableViewCell.m
//  PsychologyFM
//
//  Created by zhang on 16/1/12.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//


#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import "FMCommunityTableViewCell.h"

@implementation FMCommunityTableViewCell

//重写初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview];
    }
    return self;
}
- (void)addSubview{



#pragma mark 头像
    self.avatarButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _avatarButton.frame = CGRectMake(kMartin10, kMartin10, kScreenWidth/8, kScreenWidth/8);
    _avatarButton.backgroundColor = FMGreen;
    [self.contentView addSubview:_avatarButton];
   
#pragma mark 昵称
    self.nicknameButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.nicknameButton.frame = CGRectMake(self.avatarButton.right + 10, self.avatarButton.top, 180, 20);
    
    self.nicknameButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.nicknameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.nicknameButton setTitleColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1] forState:(UIControlStateNormal)];
    [self.contentView addSubview:_nicknameButton];
    
    [self.nicknameButton setTitleColor:FMBlack forState:(UIControlStateNormal)];
#pragma mark 时间
    self.createdLable = [[UILabel alloc]initWithFrame:CGRectMake(self.nicknameButton.left, self.nicknameButton.bottom + 5, 80, 20)];
    self.createdLable.text = @"哈哈";
    self.createdLable.font = [UIFont systemFontOfSize:14];
    self.createdLable.textColor = [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1];
    [self.contentView addSubview:_createdLable];


#pragma mark titleLable
    self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(self.avatarButton.left, self.avatarButton.bottom + 10, kScreenWidth - 20, kMartin15)];
    
    [self.contentView addSubview:_titleLable];
    
#pragma mark contentLable;
    self.contentLable = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLable.left, self.titleLable.bottom + 5, kScreenWidth - 30, 20)];
    self.contentLable.numberOfLines = -1;
    self.contentLable.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_contentLable];
    
    
//    
//    self.userImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLable.left + 0 * 110, self.contentLable.bottom + kMartin10, 100, 100)];
    
//    self.userImage2 = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLable.left + 1 * 110, self.contentLable.bottom + kMartin10, 100, 100)];
//    self.userImage3 = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLable.left + 2 * 110, self.contentLable.bottom + kMartin10, 100, 100)];
    

    
}

#pragma mark 重写model的set方法
- (void)setModel:(FMCommunityModel *)model{

    _model = model;
    [self.avatarButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.user[@"avatar"]] forState:(UIControlStateNormal)];
    [self.nicknameButton setTitle:model.user[@"nickname"] forState:(UIControlStateNormal)];
    self.titleLable.text = model.title;
    self.createdLable.text = model.created;
    
#pragma mark 自定义contentLable的高度
    
    //构建字体大小的字典
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName ,nil];
    
    CGRect frame = [model.content boundingRectWithSize:CGSizeMake(kScreenWidth - 30,CGFLOAT_MAX ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
 
    self.contentLable.frame = CGRectMake(self.titleLable.left, self.titleLable.bottom + 5, kScreenWidth - 30, frame.size.height);
    
    if (frame.size.height > 50) {
        self.contentLable.frame = CGRectMake(self.titleLable.left, self.titleLable.bottom + 5, kScreenWidth - 30, 60);
    }

    self.contentLable.text = model.content;

#pragma mark 用户上传图片
    
    if (model.images.count == 0) {
//没有图片 就移除这三个imageview;
        [self.userImage1 removeFromSuperview];
        self.userImage1 = nil;
        [self.userImage2 removeFromSuperview];
        self.userImage2 = nil;

        [self.userImage3 removeFromSuperview];
        self.userImage3 = nil;


    } else {
        [self.userImage1 removeFromSuperview];
        self.userImage1 = nil;
        [self.userImage2 removeFromSuperview];
        self.userImage2 = nil;
        
        [self.userImage3 removeFromSuperview];
        self.userImage3 = nil;
        for (int i = 0; i < model.images.count; i++) {
 
            switch (i) {
                case 0:
                {
                    
                    self.userImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLable.left + 0 * 110, self.contentLable.bottom + kMartin10, 100, 100)];
                    [self.userImage1 sd_setImageWithURL:[NSURL URLWithString:model.images[0]] placeholderImage:nil];
                    [self.contentView addSubview:_userImage1];

                }
                    break;
                case 1:
                {
                    self.userImage2 = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLable.left + 1 * 110, self.contentLable.bottom + kMartin10, 100, 100)];
                    [self.userImage2 sd_setImageWithURL:[NSURL URLWithString:model.images[1]] placeholderImage:nil];
                    [self.contentView addSubview:_userImage2];

                }
                    break;
                case 2:
                {
                    self.userImage3 = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLable.left + 2 * 110, self.contentLable.bottom + kMartin10, 100, 100)];
                    [self.userImage3 sd_setImageWithURL:[NSURL URLWithString:model.images[2]] placeholderImage:nil];
                    [self.contentView addSubview:_userImage3];

                }
                    break;
                    
                default:
                    break;
            }
            
        }
        

    }
    
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
